import requests

from bs4 import BeautifulSoup

def get_soup(url):
    headers={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.62 Safari/537.36'}
    response=requests.get(url,headers=headers)
    soup=BeautifulSoup(response.text)
    return soup

#以上步骤封装成的函数，用于获取链家租房列表的所有链接
def get_links(url):
    soup=get_soup(url)
    links_div=soup.findAll('div',class_='content__list--item')
    links=['https://bj.lianjia.com'+div.a.get('href') for div in links_div]
    return links

## 封装房屋详情
def get_house_info(house_url):
    soup=get_soup(house_url)
    price_info=soup.find('p',class_='content__aside--title')
    price=price_info.text
    info=soup.find('p',class_='content__article__table')
    area=info.contents[5].text
    layout=info.contents[3].text
    direction=info.contents[7].text
    infos={
    '价格':price,
    '面积':area,
    '户型':layout,
    '朝向':direction,
    }
    print(infos)
    return infos

if __name__=='__main__':
    url='https://bj.lianjia.com/zufang'
    links=get_links(url)
    for house_url in links:
        get_house_info(house_url)